package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.Goods;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.GoodsMapper;
import com.api.syh56.shengyunhui.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper,Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public void insert(Goods goods) {
        int insert = goodsMapper.insert(goods);
        if (insert != 1) {
            throw new BaseException("添加货物失败");
        }
    }

    @Override
    public void del(Goods goods) {
        Goods goods1 = goodsMapper.selectById(goods.getId());
        if (goods1 == null) {
            throw new BaseException("id不存在");
        }
        int del = goodsMapper.deleteById(goods);
        if (del != 1) {
            throw new BaseException("删除用户失败");
        }
    }

    @Override
    public List<Goods> selectAll() {
        List<Goods> goods = goodsMapper.selectList(null);
        if (goods.isEmpty()) {
            throw new BaseException("查询货物失败");
        }
        System.out.println(goods);
        return goods;
    }

    @Override
    public List<Goods> selectBy(Goods goods) {
        QueryWrapper<Goods> wrapper = new QueryWrapper();
        wrapper.eq("id",goods.getId());
        List<Goods> goodsList = goodsMapper.selectList(wrapper);
        if (goodsList.isEmpty()) {
            throw new BaseException("id不存在");
        }
        return goodsList;
    }

    @Override
    public Goods selectById(Long id) {
        Goods goods = goodsMapper.selectById(id);
        return goods;
    }
}
